package com.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.beans.Customer;
import com.beans.CustomerFavorite;
import com.beans.FollowRecord;
import com.beans.page.PageList;
import com.commons.util.Constants;
import com.data.mapper.BaseMapper;
import com.data.mapper.CustomerFavoriteMapper;
import com.data.mapper.CustomerMapper;
import com.data.mapper.FollowRecordMapper;
import com.services.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiangwei
 * @since 18/1/22
 */
@Service
public class CustomerServiceImpl extends BaseServiceImpl<Customer> implements CustomerService{

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private FollowRecordMapper followRecordMapper;
    @Autowired
    private CustomerFavoriteMapper customerFavoriteMapper;

    @Override
    public BaseMapper<Customer> getMapper(){return customerMapper; }

    /**
     * 重写客户分页查询
     *
     * @param pageList
     * @param bean
     * @return
     */
    public PageList<Customer> queryPageByCustomer(PageList<Customer> pageList, Customer bean) {
        Page<Customer> page;
        int pageStart;
        if (pageList.getiDisplayLength() == -1) {//不分页
            pageStart = 1;
            page = PageHelper.startPage(pageStart, 10000, false);
        } else {
            pageStart = pageList.getiDisplayStart() / pageList.getiDisplayLength() + 1;
            page = PageHelper.startPage(pageStart, pageList.getiDisplayLength());
        }
        getMapper().queryList(bean);
        pageList.setiTotalRecords(page.getTotal());
        pageList.setiTotalDisplayRecords(pageList.getiTotalRecords());

        List<Customer> customers = new ArrayList<Customer>();

        if(page.size() > 0){
            for(Customer c : page){
                //取最后一次跟进时间
                FollowRecord followRecord = new FollowRecord();

                followRecord.setRelationId(c.getCustomerId());
                followRecord.setRelationType(Constants.twoLong);//客户跟进
                //跟进记录
                List<FollowRecord> followRecords = followRecordMapper.queryList(followRecord);
                if(followRecords.size() > 0){
                    //往客户里存最后一次跟进时间
                    c.setFollowLastDate(followRecords.get(0).getUpdateDate());
                    //往客户里存最后一次跟进内容
                    c.setFollowInfo(followRecords.get(0).getText());
                }
                //查询询是否收藏
                CustomerFavorite customerFavorite = new CustomerFavorite();
                customerFavorite.setCustomerId(c.getCustomerId());
                customerFavorite.setCreateBy(bean.getCreateBy());
                List<CustomerFavorite> houseFavorites = customerFavoriteMapper.queryList(customerFavorite);

                if(houseFavorites.size() > 0){
                    c.setIsCollection(houseFavorites.get(0).getStaus());
                }

                customers.add(c);
            }
        }

        pageList.setAaData(customers);

        return pageList;
    }

    /**
     * 重写客户分页查询
     *
     * @param pageList
     * @param bean
     * @return
     */
    public PageList<Customer> queryPageNothingByCustomer(PageList<Customer> pageList, Customer bean) {
        Page<Customer> page;
        int pageStart;
        if (pageList.getiDisplayLength() == -1) {//不分页
            pageStart = 1;
            page = PageHelper.startPage(pageStart, 10000, false);
        } else {
            pageStart = pageList.getiDisplayStart() / pageList.getiDisplayLength() + 1;
            page = PageHelper.startPage(pageStart, pageList.getiDisplayLength());
        }
        getMapper().findAllList(bean);
        pageList.setiTotalRecords(page.getTotal());
        pageList.setiTotalDisplayRecords(pageList.getiTotalRecords());

        List<Customer> customers = new ArrayList<Customer>();

        if(page.size() > 0){
            for(Customer c : page){
                //取最后一次跟进时间
                FollowRecord followRecord = new FollowRecord();

                followRecord.setRelationId(c.getCustomerId());
                followRecord.setRelationType(Constants.twoLong);//客户跟进
                //跟进记录
                List<FollowRecord> followRecords = followRecordMapper.queryList(followRecord);
                if(followRecords.size() > 0){
                    //往客户里存最后一次跟进时间
                    c.setFollowLastDate(followRecords.get(0).getUpdateDate());
                    //往客户里存最后一次跟进内容
                    c.setFollowInfo(followRecords.get(0).getText());
                }
                customers.add(c);
            }
        }

        pageList.setAaData(customers);

        return pageList;
    }
}
